/*
 *
 * Copyright 2017 Asylo authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#ifndef ASYLO_PLATFORM_SYSTEM_INCLUDE_SYS_SYSMACROS_H_
#define ASYLO_PLATFORM_SYSTEM_INCLUDE_SYS_SYSMACROS_H_

#include <sys/types.h>

// Returns the major component given a device ID.
inline unsigned int major(dev_t dev) {
  return ((dev >> 8) & 0xfff) | ((unsigned int)(dev >> 32) & ~0xfff);
}

// Returns the minor component given a device ID.
inline unsigned int minor(dev_t dev) {
  return (dev & 0xff) | ((unsigned int)(dev >> 12) & ~0xff);
}

// Combines major and minor device IDs to produce a device ID.
inline dev_t makedev(unsigned int major, unsigned int minor) {
  return ((minor & 0xff) | ((major & 0xfff) << 8) |
          (((uint64_t)(minor & ~0xff)) << 12) |
          (((uint64_t)(major & ~0xfff)) << 32));
}

#endif  // ASYLO_PLATFORM_SYSTEM_INCLUDE_SYS_SYSMACROS_H_
